package com.ssyc.gsk_master.view;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.gsk_master.R;


/**
 * Created by Administrator on 2017/12/12 0012.
 * 自定义底部导航栏的切换按钮
 */

public class BottomBar extends LinearLayout {

    //底部View的视图
    private View bottomView;

    //被选中的图片资源
    private int[] selectImgs;

    //没有被选中的图片资源
    private int[] normalImgs;

    //导航栏的文字
    private String[] tabTexts;

    //被选中的文字颜色
    private int selectColor = getResources().getColor(R.color.tab_select_color);

    //未被选中的文字颜色
    private int normalColor = getResources().getColor(R.color.tab_normal_color);

    //Tab栏的宽和高度
    private int itemSize = 42;

    //回调接口
    private onTabSelectedListener listener;

    //线性布局参数
    private LayoutParams lp, lvP;

    public BottomBar(Context context) {
        super(context);
        init();
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    /**
     * 初始化视图的方法
     */
    private void init() {
        if (null != selectImgs && selectImgs.length != 0 && null != normalImgs && normalImgs.length != 0 && null != tabTexts && tabTexts.length != 0) {
            //设置为横向的布局
            setOrientation(HORIZONTAL);

            //设置布局参数
            lp = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
            lvP = new LayoutParams(dp2px(getContext(), itemSize), dp2px(getContext(), (int) (itemSize*1.2)));
            //第一步:动态添加均分的线性布局
            for (int i = 0; i < selectImgs.length; i++) {
                final int j = i;
                //获取底部的view
                bottomView = View.inflate(getContext(), R.layout.master_tab_bottom, null);


                final ImageView lv = bottomView.findViewById(R.id.lv);
                lv.setLayoutParams(lvP);

//                TextView tv = bottomView.findViewById(R.id.tv);
//                tv.setText(tabTexts[i]);


                final LinearLayout llOut = bottomView.findViewById(R.id.ll_out);
                llOut.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        //做动画处理
                        startShakeByPropertyAnim(llOut, 0.5f, 1f, 10, 1000);

                        //同步状态
                        changeState(j);

                        //回调选中
                        if (listener != null) {
                            listener.onSelected(j);
                        }
                    }
                });

                //初次加载的时候隐藏了显示数字的TextView
                TextView tvNum = bottomView.findViewById(R.id.tv_num);
                tvNum.setVisibility(View.GONE);

                if (i == 0) {
                    lv.setImageResource(selectImgs[0]);
//                    tv.setTextColor(selectColor);
                } else {
                    lv.setImageResource(normalImgs[i]);
//                    tv.setTextColor(normalColor);
                }
                addView(bottomView, lp);
            }
        }
    }


    /**
     * 根据选中的位置同步状态的方法
     *
     * @param pos
     */
    private void changeState(int pos) {
        //获取tab的数量
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            RelativeLayout rlOut = (RelativeLayout) getChildAt(i);
//            TextView tv = rlOut.findViewById(R.id.tv);
            ImageView lv = rlOut.findViewById(R.id.lv);
            if (pos == i) {
                lv.setImageResource(selectImgs[i]);
//                tv.setTextColor(selectColor);
            } else {
                lv.setImageResource(normalImgs[i]);
//                tv.setTextColor(normalColor);
            }
        }
    }


    /**
     * @param pos  需要显示小红点的位置
     * @param text 需要显示小红点的文字,一般用于显示消息
     */
    public void showNum(int pos, String text) {
        //获取tab的数量
        RelativeLayout rlOut = (RelativeLayout) getChildAt(pos);
        TextView tvNum = rlOut.findViewById(R.id.tv_num);
        if (tvNum.getVisibility() != View.VISIBLE) {
            tvNum.setVisibility(View.VISIBLE);
            tvNum.setText(text);
        }
    }


    /**
     * @param pos 需要显示小红点的位置,一般当消息为0时隐藏
     */
    public void closeNum(int pos) {
        //获取tab的数量
        RelativeLayout rlOut = (RelativeLayout) getChildAt(pos);
        TextView tvNum = rlOut.findViewById(R.id.tv_num);
        if (tvNum.getVisibility() != View.GONE) {
            tvNum.setVisibility(View.GONE);
        }
    }

    /**
     * 回调接口
     */
    public interface onTabSelectedListener {
        void onSelected(int pos);
    }

    /**
     * 设置监听的方法
     *
     * @param onSelectedListener
     */
    public void setOnSelectedListener(onTabSelectedListener onSelectedListener) {
        this.listener = onSelectedListener;
    }


    /**
     * 定义将dp转化为px的方法
     *
     * @param dp
     * @return
     */
    public int dp2px(Context context, int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }


    /**
     * 实现抖动动画效果
     *
     * @param view         进行动画的视图对象
     * @param scaleSmall   缩放的下限
     * @param scaleLarge   缩放的上限
     * @param shakeDegrees 抖动的角度
     * @param duration     动画执行的时间
     */
    private void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
        if (view == null) {
            return;
        }

        //先变小后变大
        PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        );

        PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        );

        //先往左再往右
        PropertyValuesHolder rotateValuesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(0.1f, -shakeDegrees),
                Keyframe.ofFloat(0.2f, shakeDegrees),
                Keyframe.ofFloat(0.3f, -shakeDegrees),
                Keyframe.ofFloat(0.4f, shakeDegrees),
                Keyframe.ofFloat(0.5f, -shakeDegrees),
                Keyframe.ofFloat(0.6f, shakeDegrees),
                Keyframe.ofFloat(0.7f, -shakeDegrees),
                Keyframe.ofFloat(0.8f, shakeDegrees),
                Keyframe.ofFloat(0.9f, -shakeDegrees),
                Keyframe.ofFloat(1.0f, 0f)
        );

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, rotateValuesHolder);
        objectAnimator.setDuration(duration);
        objectAnimator.start();
    }

    /**
     * 设置数据的方法
     *
     * @param selectImgs
     * @param tabTexts
     */
    public void setData(int[] selectImgs, int[] normalImgs, String[] tabTexts) {
        this.selectImgs = selectImgs;
        this.normalImgs = normalImgs;
        this.tabTexts = tabTexts;
        //先移除所有的孩子
        if (getChildCount() != 0) {
            removeAllViews();
        }
        init();
    }
}
